package ywh.stream
{
	import ywh.event.MediaListEvent;
	import ywh.event.StreamEvent;
	import ywh.event.SyncEvent;
	import ywh.list.CourseItem;
	import ywh.list.MediaList;
	import ywh.manager.Core;
	import ywh.manager.Sync;
	import ywh.player.AbstractPlayer;
	import ywh.player.AbstractShower;
	import ywh.pool.PlayerPool;
	
	/**
	 * 媒体流类;
	 * @author 杨伟华
	 * @version 1.0
	 *
	 * Created 1/21/2013 3:27 AM
	 */
	public class MediaStream
	{
		protected var _list:MediaList;
		protected var _shower:AbstractShower;
		protected var _isFixedPlayer:Boolean;
		
		protected var _currentItem:CourseItem;
		protected var _currentIndex:int;
		protected var _nextAvilable:Boolean;
		protected var _prevAvilable:Boolean;
		
		protected var _syncPlayer:Boolean;
		
		protected var _streamIsReady:Boolean = false;
		protected var _streamIsActive:Boolean = true;
		protected var _showerIsActive:Boolean = false;
		
		public function MediaStream()
		{
			_currentItem = new CourseItem();
		}
		
		public function initStream(list:MediaList, defaultShower:AbstractShower = null)
		{
			destoryStream();
			if (defaultShower)
			{
				_isFixedPlayer = true;
				_shower = defaultShower;
			}
			else
			{
				_isFixedPlayer = false;
			}
			_list = list;
			_currentIndex = -1;
			_currentItem.defaultData = list.xml;
			Core.addEventListener(SyncEvent.PRE_SYNC, handlePreSync);
			Core.addEventListener(SyncEvent.SYNC_LIST, syncList);
			Core.addEventListener(SyncEvent.SYNC_PLAYER, syncPlayer);
			_streamIsReady = true;
		}
		
		public function destoryStream():void
		{
			if (_streamIsReady)
			{
				Core.removeEventListener(SyncEvent.PRE_SYNC, handlePreSync);
				Core.removeEventListener(SyncEvent.SYNC_LIST, syncList);
				Core.removeEventListener(SyncEvent.SYNC_PLAYER, syncPlayer);
				_list = null;
				_shower = null;
				_streamIsReady = false;
				_currentIndex = -1;
				_currentItem.data = null;
				_currentItem.defaultData = null;
			}
		}
		
		/**
		 * 对于主要流：计算Player状态，当前时间；是否阻止一次全局同步;
		 * 对于次要流：计算Player状态，当前时间；是否阻止当前这次Player的同步；
		 * @param	e
		 */
		protected function handlePreSync(e:SyncEvent):void
		{
			
		}
		
		/**
		 * 根据当前课程时间，查询是否需要List跳转;
		 * 如果list跳转了，则设置Player内容，同样阻止当前这次Player的同步;
		 * 对于次要流,如果流为不活跃状态,则不会同步跳转,也不会同步Player;
		 * @param	e
		 */
		protected function syncList(e:SyncEvent):void
		{
			var streamTime:Number = e.time;
			if (!currentItem.data || streamTime < currentItem.startTime || streamTime > currentItem.endTime)
			{
				var index:int = _list.getIndexByStartTime(streamTime);
				_syncPlayer = !changeItemTo(index);
			}
		}
		
		/**
		 * 如果为主要流；什么都不做；如果为次要流，则设置Player时间；
		 * @param	e
		 */
		protected function syncPlayer(e:SyncEvent):void
		{
		
		}
		
		/**
		 * 更改当前项目;
		 * 返回是否更改成功;
		 * @param	index
		 * @param	forPlay
		 * @return
		 */
		public function changeItemTo(index:int):void
		{
			if (canChangeIndexTo(index))
			{
				_currentIndex = index;
				_currentItem.data = _list.getItemAt(index);
				var tmpEndTime:Number = _list.getItemStartTime(index + 1);
				_currentItem.endTime = isNaN(tmpEndTime) ? totalDuration : tmpEndTime;
				if (!_isFixedPlayer)
				{
					_shower = PlayerPool.getPlayerById(currentItem.playerId);
				}
				_shower.playItem = currentItem;
				_nextAvilable = canChangeIndexTo(index + 1);
				_prevAvilable = canChangeIndexTo(index - 1);
				Core.dispatchEvent(new StreamEvent(StreamEvent.STREAM_LIST_CHANGE_ITEM, this));
			}
		}
		
		/**
		 * 是否可以跳转到制定的编号;
		 * @param	index
		 * @return
		 */
		public function canChangeIndexTo(index:int):Boolean
		{
			return (index != currentIndex) && (_list.isAvilableIndex(index));
		}
		
		/**
		 * 跳转到下一个项目;
		 */
		public function nextItem():void
		{
			if (nextAvilable)
			{
				MainStream.$.time = (_list.getItemStartTime(currentIndex + 1));
			}
		}
		
		/**
		 * 跳转到上一个项目;
		 */
		public function prevItem():void
		{
			if (prevAvilable)
			{
				MainStream.$.time = (_list.getItemStartTime(currentIndex - 1));
			}
		}
		
		/**
		 * 是否可以跳转到下一个项目;
		 */
		public function get nextAvilable():Boolean
		{
			return _nextAvilable;
		}
		
		/**
		 * 是否可以跳转到上一个项目;
		 */
		public function get prevAvilable():Boolean
		{
			return _prevAvilable;
		}
		
		/**
		 * 获得当前项目信息;
		 */
		public function get currentItem():CourseItem
		{
			return _currentItem;
		}
		
		/**
		 * 返回当前的编号;
		 */
		public function get currentIndex():int
		{
			return _currentIndex;
		}
		
		/**
		 * 最大序号;
		 */
		public function get count():uint
		{
			return _list.count;
		}
		
		public function get totalDuration():Number
		{
			return _list.totalDuration;
		}
		
		/**
		 * 获取Player;
		 */
		public function get shower():AbstractShower
		{
			return _shower;
		}
		
		public function get showerIsActive():Boolean
		{
			return _showerIsActive;
		}
		
		public function set showerIsActive(value:Boolean):void
		{
			_showerIsActive = value;
		}
		
		public function get streamIsActive():Boolean
		{
			return _streamIsActive;
		}
		
		public function set streamIsActive(value:Boolean):void
		{
			_streamIsActive = value;
		}
	
	}

}